package cn.snmp.mutual.adapter;

import java.util.ResourceBundle;
import cn.snmp.mutual.nms.SnmpVar;
import cn.snmp.mutual.nms.util.SnmpTarget;
import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.util.DumpUtil;

/**
 * @author dengkn
 * MIB NODE Adapter ，extends GeneralMibNodeAdapter
 * 在读取和写入snmp mib node 的时候实现readMibData、writeMibData方法
 * GeneralMibNodeAdapter 默认实现类
 */
public abstract class AbstractMibNodeAdapter {
	public static final String MIB_NODE_ADAPTER = "MibNodeAdapter";
	public static final String CALSS = "Class";
	
	protected ObjService option = null;
	protected ObjService rowSet = null;

	public void setOption(ObjService option) {
		this.option = option;
	}

	public ObjService getAdapterInfo(ObjService mibnodeThreadLocal ) {
		return mibnodeThreadLocal.objectSize(MIB_NODE_ADAPTER) > 0 ? mibnodeThreadLocal.objectAt(MIB_NODE_ADAPTER, 0) : mibnodeThreadLocal;
	}

	public void setRowSet(ObjService rowSet) {
		this.rowSet = rowSet;
	}

	public static AbstractMibNodeAdapter getMibNodeAdapter(ObjService mibnode) {
		Object mibNodeAdapter = null;
		try {
			String e = mibnode.getStringValue(MIB_NODE_ADAPTER);
			if ("".equals(e) && mibnode.objectSize(MIB_NODE_ADAPTER) > 0) {
				e = mibnode.objectAt(MIB_NODE_ADAPTER, 0).getStringValue(CALSS);
			}
			if ("".equals(e)) {
				mibNodeAdapter = new GeneralMibNodeAdapter();
			} else {
				mibNodeAdapter =  Class.forName(e).newInstance();
				DumpUtil.debug("Construct [" + e + "] successed...");
			}

			return (AbstractMibNodeAdapter) mibNodeAdapter;
		} catch (Exception arg2) {
			DumpUtil.debug(arg2);
			return (AbstractMibNodeAdapter) mibNodeAdapter;
		}
	}

	public static AbstractMibNodeAdapter getMibNodeAdapter(ObjService mibnode, ObjService option) {
		Object mibNodeAdapter = null;

		try {
			String e = mibnode.getStringValue(MIB_NODE_ADAPTER);
			if ("".equals(e) && mibnode.objectSize(MIB_NODE_ADAPTER) > 0) {
				e = mibnode.objectAt(MIB_NODE_ADAPTER, 0).getStringValue(CALSS);
			}

			if ("".equals(e)) {
				mibNodeAdapter = new GeneralMibNodeAdapter();
			} else {
				mibNodeAdapter =  Class.forName(e).newInstance();
				DumpUtil.debug("Construct [" + e + "] successed...");
			}

			((AbstractMibNodeAdapter) mibNodeAdapter).setOption(option);
			return (AbstractMibNodeAdapter) mibNodeAdapter;
		} catch (Exception arg3) {
			DumpUtil.debug(arg3);
			return (AbstractMibNodeAdapter) mibNodeAdapter;
		}
	}

	public void getMibNodeInfo(SnmpTarget stTarget, ResourceBundle rbResource, ObjService osTableInfo,
							   ObjService osMibNode, ObjService osResult, ObjService osParams) {
	}

	public Object nativeReadMibData(SnmpVar var, ObjService mibnode) {
		return this.readMibData(var, mibnode);
	}

	public SnmpVar nativeWriteMibData(Object value, ObjService mibnode) {
		return this.writeMibData((String) value, mibnode);
	}
	
	/**
	 * Mib Node Adapter or Conversion type
	 * @param var data value var
	 * @param mibnode mib node Objservice 
	 * @return value String
	 * */
	public abstract String readMibData(SnmpVar var, ObjService mibnode);

	/**
	 * Mib Node Adapter or Conversion type
	 * @param var data value var
	 * @param mibnode mib node Objservice 
	 * @return value String
	 * */
	public abstract SnmpVar writeMibData(String var, ObjService mibnode);
}